package PL.practica3;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;

import PL.practica3.AnasintTerm;
import PL.practica3.Anasint;
import PL.practica3.Analex;
import antlr.ANTLRException;
public class Procesador {
	

	public static void main(String args[]) {
		if (args.length != 0){
			try {
				FileInputStream fis = new FileInputStream(args[0]);
				Analex analex = null;
				Anasint anasint = null;
				analex = new Analex(fis);
				//				Token token = analex.nextToken();
				//				while(token.getType() != Token.EOF_TYPE) { 
				//					System.out.println(token);
				//					token = analex.nextToken(); 
				//					}
				anasint = new Anasint(analex);
				anasint.entrada();
			}catch(ANTLRException ae) {
				System.err.println(ae.getMessage());
			}
			catch(FileNotFoundException fnfe) {
				System.err.println("No se encontró el fichero");
			}
		}
		else{
			try {
				InputStreamReader isr =
						new InputStreamReader(System.in);
				BufferedReader br = new BufferedReader(isr);
				String linea = br.readLine();
				while (!linea.equals("$")) {
					Analex analex = null;
					AnasintTerm anasintTerm = null;
					analex = new Analex(new StringReader(linea));
					anasintTerm = new AnasintTerm(analex);
					anasintTerm.entrada();
					linea = br.readLine();
				}
			}
			catch(ANTLRException ae) {
				System.err.println(ae.getMessage());
			}
			catch(IOException ioe) {
				System.err.println(ioe.getMessage());
			}
		}
	}

}
